package com.ggb.reactive.service

import com.ggb.reactive.domain.entity.OrderDetail
import com.ggb.reactive.extension.log
import lombok.RequiredArgsConstructor
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
import java.math.BigDecimal

@Service
@RequiredArgsConstructor
class OrderDetailService(
    private val databaseClient: DatabaseClient
) {

    fun findAllByOrderId(id: Long): Flux<OrderDetail> {
        return databaseClient.sql("SELECT * FROM order_detail WHERE order_id = :id")
            .bind(0, id)
            .map { row, _ ->
                OrderDetail(
                    id = row.get("id", Long::class.java),
                    number = row.get("number", Int::class.java),
                    name = row.get("name", String::class.java),
                    orderId = row.get("order_id", Long::class.java),
                    dishId = row.get("dish_id", Long::class.java),
                    setmealId = row.get("setmeal_id", Int::class.java),
                    dishFlavor = row.get("dish_flavor", String::class.java),
                    amount = row.get("amount", BigDecimal::class.java),
                    image = row.get("image", String::class.java)
                )
            }
            .all()
    }

}